package mvc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mvc.action.Action;
import mvc.form.ActionForm;
import mvc.map.ActionMapping;
import mvc.util.FormUtils;

public class ActionServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
   
    public ActionServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		ActionForm form = FormUtils.primeForm(request);
		Action action = null;
		try{
			Class clazz = Class.forName(ActionMapping.getActionMap().get(request.getParameter("sign")));
			action = (Action) clazz.newInstance();
		}catch (Exception e) {
			e.printStackTrace();
		}
		String msg = action.excute(form);
		PrintWriter out = response.getWriter();
		out.append(msg);
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
